package com.macro.mall.portal.config;

import com.macro.mall.portal.domain.QueueEnum;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 消息队列相关配置
 * Created by macro on 2018/9/14.
 */
@Configuration
public class RabbitMqConfig {
    //取消订单
    /**
     * 订单消息实际消费队列所绑定的交换机
     */
    @Bean
    DirectExchange orderDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_ORDER_CANCEL.getExchange())
                .durable(true)
                .build();
    }

    /**
     * 订单延迟队列队列所绑定的交换机
     */
    @Bean
    DirectExchange orderTtlDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_TTL_ORDER_CANCEL.getExchange())
                .durable(true)
                .build();
    }

    /**
     * 订单实际消费队列
     */
    @Bean
    public Queue orderQueue() {
        return new Queue(QueueEnum.QUEUE_ORDER_CANCEL.getName());
    }

    /**
     * 订单延迟队列（死信队列）
     */
    @Bean
    public Queue orderTtlQueue() {
        return QueueBuilder
                .durable(QueueEnum.QUEUE_TTL_ORDER_CANCEL.getName())
                //到期后转发的交换机
                .withArgument("x-dead-letter-exchange", QueueEnum.QUEUE_ORDER_CANCEL.getExchange())
                //到期后转发的路由键
                .withArgument("x-dead-letter-routing-key", QueueEnum.QUEUE_ORDER_CANCEL.getRouteKey())
                .build();
    }

    /**
     * 将订单队列绑定到交换机
     */
    @Bean
    Binding orderBinding(DirectExchange orderDirect,Queue orderQueue){
        return BindingBuilder
                .bind(orderQueue)
                .to(orderDirect)
                .with(QueueEnum.QUEUE_ORDER_CANCEL.getRouteKey());
    }

    /**
     * 将订单延迟队列绑定到交换机
     */
    @Bean
    Binding orderTtlBinding(DirectExchange orderTtlDirect,Queue orderTtlQueue){
        return BindingBuilder
                .bind(orderTtlQueue)
                .to(orderTtlDirect)
                .with(QueueEnum.QUEUE_TTL_ORDER_CANCEL.getRouteKey());
    }

    //创建分销订单
    /**
     * 创建分销订单队列所绑定的交换机
     */
    @Bean
    DirectExchange distributionCreatDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_DISTRIBUTION_CREAT.getExchange())
                .durable(true)
                .build();
    }

    /**
     * 创建分销订单延迟队列队列所绑定的交换机
     */
    @Bean
    DirectExchange distributionCreatTtlDirect() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_TTL_DISTRIBUTION_CREAT.getExchange())
                .durable(true)
                .build();
    }

    /**
     *  创建分销订单实际消费队列
     */
    @Bean
    public Queue distributionCreatQueue() {
        return new Queue(QueueEnum.QUEUE_DISTRIBUTION_CREAT.getName());
    }

    /**
     *  创建分销订单延迟队列（死信队列）
     */
    @Bean
    public Queue distributionCreatTtlQueue() {
        return QueueBuilder
                .durable(QueueEnum.QUEUE_TTL_DISTRIBUTION_CREAT.getName())
                //到期后转发的交换机
                .withArgument("x-dead-letter-exchange", QueueEnum.QUEUE_DISTRIBUTION_CREAT.getExchange())
                //到期后转发的路由键
                .withArgument("x-dead-letter-routing-key", QueueEnum.QUEUE_DISTRIBUTION_CREAT.getRouteKey())
                .build();
    }

    /**
     * 将创建分销订单队列绑定到交换机
     */
    @Bean
    Binding distributionCreatBinding(DirectExchange distributionCreatDirect,Queue distributionCreatQueue){
        return BindingBuilder
                .bind(distributionCreatQueue)
                .to(distributionCreatDirect)
                .with(QueueEnum.QUEUE_DISTRIBUTION_CREAT.getRouteKey());
    }

    /**
     * 将创建分销订单延迟队列绑定到交换机
     */
    @Bean
    Binding distributionCreatTtlBinding(DirectExchange distributionCreatTtlDirect,Queue distributionCreatTtlQueue){
        return BindingBuilder
                .bind(distributionCreatTtlQueue)
                .to(distributionCreatTtlDirect)
                .with(QueueEnum.QUEUE_TTL_DISTRIBUTION_CREAT.getRouteKey());
    }
    //取消团购
    /**
     * 订单消息实际消费队列所绑定的交换机
     */
    @Bean
    DirectExchange groupBuyCancel() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_GROUP_BUY_CANCEL.getExchange())
                .durable(true)
                .build();
    }

    /**
     * 订单延迟队列队列所绑定的交换机
     */
    @Bean
    DirectExchange groupBuyTtlCancel() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_TTL_GROUP_BUY_CANCEL.getExchange())
                .durable(true)
                .build();
    }

    /**
     * 订单实际消费队列
     */
    @Bean
    public Queue groupBuyQueue() {
        return new Queue(QueueEnum.QUEUE_GROUP_BUY_CANCEL.getName());
    }

    /**
     * 订单延迟队列（死信队列）
     */
    @Bean
    public Queue groupBuyTtlQueue() {
        return QueueBuilder
                .durable(QueueEnum.QUEUE_TTL_GROUP_BUY_CANCEL.getName())
                //到期后转发的交换机
                .withArgument("x-dead-letter-exchange", QueueEnum.QUEUE_GROUP_BUY_CANCEL.getExchange())
                //到期后转发的路由键
                .withArgument("x-dead-letter-routing-key", QueueEnum.QUEUE_GROUP_BUY_CANCEL.getRouteKey())
                .build();
    }

    /**
     * 将订单队列绑定到交换机
     */
    @Bean
    Binding groupBuyBinding(DirectExchange groupBuyCancel,Queue groupBuyQueue){
        return BindingBuilder
                .bind(groupBuyQueue)
                .to(groupBuyCancel)
                .with(QueueEnum.QUEUE_GROUP_BUY_CANCEL.getRouteKey());
    }

    /**
     * 将订单延迟队列绑定到交换机
     */
    @Bean
    Binding groupBuyTtlBinding(DirectExchange groupBuyTtlCancel,Queue groupBuyTtlQueue){
        return BindingBuilder
                .bind(groupBuyTtlQueue)
                .to(groupBuyTtlCancel)
                .with(QueueEnum.QUEUE_TTL_GROUP_BUY_CANCEL.getRouteKey());
    }
//取消秒杀订单
    /**
     * 订单消息实际消费队列所绑定的交换机
     */
    @Bean
    DirectExchange secKillCancel() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_SEC_KILL_CANCEL.getExchange())
                .durable(true)
                .build();
    }
    /**
     * 订单延迟队列队列所绑定的交换机
     */
    @Bean
    DirectExchange secKillTtlCancel() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_TTL_SEC_KILL_CANCEL.getExchange())
                .durable(true)
                .build();
    }

    /**
     * 订单实际消费队列
     */
    @Bean
    public Queue secKillQueue() {
        return new Queue(QueueEnum.QUEUE_SEC_KILL_CANCEL.getName());
    }

    /**
     * 订单延迟队列（死信队列）
     */
    @Bean
    public Queue secKillTtlQueue() {
        return QueueBuilder
                .durable(QueueEnum.QUEUE_TTL_SEC_KILL_CANCEL.getName())
                //到期后转发的交换机
                .withArgument("x-dead-letter-exchange", QueueEnum.QUEUE_SEC_KILL_CANCEL.getExchange())
                //到期后转发的路由键
                .withArgument("x-dead-letter-routing-key", QueueEnum.QUEUE_SEC_KILL_CANCEL.getRouteKey())
                .build();
    }

    /**
     * 将订单队列绑定到交换机
     */
    @Bean
    Binding secKillBinding(DirectExchange secKillCancel,Queue secKillQueue){
        return BindingBuilder
                .bind(secKillQueue)
                .to(secKillCancel)
                .with(QueueEnum.QUEUE_SEC_KILL_CANCEL.getRouteKey());
    }

    /**
     * 将订单延迟队列绑定到交换机
     */
    @Bean
    Binding secKillTtlBinding(DirectExchange secKillTtlCancel,Queue secKillTtlQueue){
        return BindingBuilder
                .bind(secKillTtlQueue)
                .to(secKillTtlCancel)
                .with(QueueEnum.QUEUE_TTL_SEC_KILL_CANCEL.getRouteKey());
    }
    //取消团购订单
    /**
     * 订单消息实际消费队列所绑定的交换机
     */
    @Bean
    DirectExchange groupBuyOrderCancel() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_GROUP_BUY_ORDER_CANCEL.getExchange())
                .durable(true)
                .build();
    }

    /**
     * 订单延迟队列队列所绑定的交换机
     */
    @Bean
    DirectExchange groupBuyOrderTtlCancel() {
        return (DirectExchange) ExchangeBuilder
                .directExchange(QueueEnum.QUEUE_TTL_GROUP_BUY_ORDER_CANCEL.getExchange())
                .durable(true)
                .build();
    }

    /**
     * 订单实际消费队列
     */
    @Bean
    public Queue groupBuyOrderQueue() {
        return new Queue(QueueEnum.QUEUE_GROUP_BUY_ORDER_CANCEL.getName());
    }

    /**
     * 订单延迟队列（死信队列）
     */
    @Bean
    public Queue groupBuyOrderTtlQueue() {
        return QueueBuilder
                .durable(QueueEnum.QUEUE_TTL_GROUP_BUY_ORDER_CANCEL.getName())
                //到期后转发的交换机
                .withArgument("x-dead-letter-exchange", QueueEnum.QUEUE_GROUP_BUY_ORDER_CANCEL.getExchange())
                //到期后转发的路由键
                .withArgument("x-dead-letter-routing-key", QueueEnum.QUEUE_GROUP_BUY_ORDER_CANCEL.getRouteKey())
                .build();
    }

    /**
     * 将订单队列绑定到交换机
     */
    @Bean
    Binding groupBuyOrderBinding(DirectExchange groupBuyOrderCancel,Queue groupBuyOrderQueue){
        return BindingBuilder
                .bind(groupBuyOrderQueue)
                .to(groupBuyOrderCancel)
                .with(QueueEnum.QUEUE_GROUP_BUY_ORDER_CANCEL.getRouteKey());
    }

    /**
     * 将订单延迟队列绑定到交换机
     */
    @Bean
    Binding groupBuyOrderTtlBinding(DirectExchange groupBuyOrderTtlCancel,Queue groupBuyOrderTtlQueue){
        return BindingBuilder
                .bind(groupBuyOrderTtlQueue)
                .to(groupBuyOrderTtlCancel)
                .with(QueueEnum.QUEUE_TTL_GROUP_BUY_ORDER_CANCEL.getRouteKey());
    }
}
